package com.xinjian.jianba.service.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xinjian.jianba.dao.dwmid.MomCommentDao;
import com.xinjian.jianba.dao.dwmid.MomlikeDao;
import com.xinjian.jianba.dao.dwmid.UserMediaDao;
import com.xinjian.jianba.dao.dwmid.UserMonmentsDao;
import com.xinjian.jianba.dto.MonsDto;
import com.xinjian.jianba.dto.MonsIdDto;
import com.xinjian.jianba.dto.MonsVo;
import com.xinjian.jianba.dto.UserMonmentsDto;
import com.xinjian.jianba.dto.UserMonmentsVo;
import com.xinjian.jianba.dto.remindVo;
import com.xinjian.jianba.enity.MomCommentEntity;
import com.xinjian.jianba.enity.UserMonmentsEntity;
import com.xinjian.jianba.service.UserMonmentsService;
import com.xinjian.jianba.service.VideoService;
import com.xinjian.jianba.utils.QiNiuUtil;
import com.xinjian.jianba.utils.TimeUtil;
import com.xinjian.jianba.utils.Utils;
@Service
public class UserMonmentsServiceImpl  implements UserMonmentsService{

	@Autowired
	private UserMonmentsDao userMonmentsDao;
	@Autowired
	private MomCommentDao momCommentDao;
	@Autowired
	private MomlikeDao momlikeDao;
	@Autowired
	private UserMediaDao userMediaDao;
	@Autowired
	private VideoService videoService;
	@Value("${jianba.vod.imgStr}")
	private String imgStr;

	@Override
	public Map<String,Object> getMons(MonsDto monsDto) throws ParseException {
		List<UserMonmentsEntity> list=userMonmentsDao.findMommentsAndNickname(monsDto);
		List<MonsVo> resultList=new ArrayList<MonsVo>();
		Map<String, Object> map=new HashMap<String,Object>();
		int pageNumber=monsDto.getPageNumber().intValue();
		if(pageNumber==1){
			map.put("UnReadNum", String.valueOf(momCommentDao.findUnreadCommentAndReplayCounts(monsDto.getUserId())));		
		}else{
			map.put("UnReadNum", "");
		}
		for(UserMonmentsEntity userMonmentsEntity:list){
			MonsVo monsVo=new MonsVo();
			monsVo.setMonId(userMonmentsEntity.getMonId());
			monsVo.setAddTime(TimeUtil.getTimeFormatText(Utils.convertTimestampToString(userMonmentsEntity.getAddTime())));
			monsVo.setContent(userMonmentsEntity.getDetail());
			monsVo.setUrl(userMonmentsEntity.getMedia_adress());
			monsVo.setUserId(userMonmentsEntity.getUserId());
			monsVo.setUserName((userMonmentsEntity.getUserInfoEnity()==null)?"":userMonmentsEntity.getUserInfoEnity().getNickname());
			monsVo.setMomLikeNum(momlikeDao.findLikeCountsByMonId(userMonmentsEntity.getMonId()));
			monsVo.setMomCommentNum(momCommentDao.findCommentCountsByMonId(userMonmentsEntity.getMonId()));
			monsVo.setLogoAdress((userMonmentsEntity.getUserInfoEnity()==null)?"":(userMonmentsEntity.getUserInfoEnity().getUserLogo()==null?"":userMonmentsEntity.getUserInfoEnity().getUserLogo()));
			String imgStr=userMonmentsEntity.getImgStr();
			if(StringUtils.isBlank(imgStr)){
				monsVo.setImgStr(this.imgStr);
			}else if("1".equals(imgStr)){
				monsVo.setImgStr("");
			}else{
				monsVo.setImgStr(imgStr);
			}
			Map<String,Long> likeMap=new HashMap<String,Long>();
			likeMap.put("monId",userMonmentsEntity.getMonId());
			likeMap.put("userId", monsDto.getUserId());
			Long num=momlikeDao.isLikeByMonidAnduserId(likeMap);
			if(num==0){
				monsVo.setIsLike("0");
			}else{
				monsVo.setIsLike("1");
			}
			
			resultList.add(monsVo);
		}
		map.put("result", resultList);
		return map;
	}

	@Override
	public List<MonsVo> getPersonMons(MonsIdDto monsDto) throws ParseException {
		List<UserMonmentsEntity> list=userMonmentsDao.getPersonMons(monsDto);
		List<MonsVo> resultList=new ArrayList<MonsVo>();
		for(UserMonmentsEntity userMonmentsEntity:list){
			MonsVo monsVo=new MonsVo();
			monsVo.setMonId(userMonmentsEntity.getMonId());
			monsVo.setAddTime(TimeUtil.getTimeFormatText(Utils.convertTimestampToString(userMonmentsEntity.getAddTime())));
			monsVo.setContent(userMonmentsEntity.getDetail());
			monsVo.setUrl(userMonmentsEntity.getMedia_adress());
			monsVo.setUserId(userMonmentsEntity.getUserId());
			monsVo.setUserName((userMonmentsEntity.getUserInfoEnity()==null)?"":userMonmentsEntity.getUserInfoEnity().getNickname());
			monsVo.setMomLikeNum(momlikeDao.findLikeCountsByMonId(userMonmentsEntity.getMonId()));
			monsVo.setMomCommentNum(momCommentDao.findCommentCountsByMonId(userMonmentsEntity.getMonId()));
			monsVo.setLogoAdress((userMonmentsEntity.getUserInfoEnity()==null)?"":(userMonmentsEntity.getUserInfoEnity().getUserLogo()==null?"":userMonmentsEntity.getUserInfoEnity().getUserLogo()));
			//第一帧截图
			//imgStr为1则为图片
			if(StringUtils.isBlank(userMonmentsEntity.getImgStr())){
				monsVo.setImgStr(imgStr);
			}else if("1".equals(userMonmentsEntity.getImgStr())){
				monsVo.setImgStr("");
			}else{
				monsVo.setImgStr(userMonmentsEntity.getImgStr());
			}
			Map<String,Long> likeMap=new HashMap<String,Long>();
			likeMap.put("monId",userMonmentsEntity.getMonId());
			likeMap.put("userId", monsDto.getWatchId());
			Long num=momlikeDao.isLikeByMonidAnduserId(likeMap);
			if(num==0){
				monsVo.setIsLike("0");
			}else{
				monsVo.setIsLike("1");
			}
			resultList.add(monsVo);
		}
		return resultList;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public int delete(Long monId,String address) {
		UserMonmentsEntity userMonmentsEntity=new UserMonmentsEntity();
		userMonmentsEntity.setMonId(monId);
		List<UserMonmentsEntity> list=userMonmentsDao.select(userMonmentsEntity);
		String videoId=null;
		if(list.size()>0){
			videoId=list.get(0).getMedia_adress();
		}
		int k=userMonmentsDao.delete_monments(monId);
		momCommentDao.deleteMonMomments(monId);
		momlikeDao.deleteMonsLike(monId);
		if(k>0&&!address.equals("")&&address.lastIndexOf(".mp4")<0){
			String keyAddress=address.substring(address.indexOf(QiNiuUtil.domainOfBucket)+1+QiNiuUtil.domainOfBucket.length());
			QiNiuUtil.delete(new String[]{keyAddress});
		}
		if(k>0&&!address.equals("")&&address.lastIndexOf(".mp4")>0){
			if(videoId!=null||!videoId.equals("")){
			videoService.deleteVod(videoId);
			}
		}
		return k;
	}

	@Override
	public int findCountByMonId(Long monId) {
		int count=userMonmentsDao.findCountByMonId(monId);
		return count;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public List<remindVo> remindDetailAndUpdate(Long userId) throws ParseException {
		List<MomCommentEntity> list=momCommentDao.remindDetail(userId);
		List<remindVo> reList=new ArrayList<remindVo>();
		String type=null;
		for(MomCommentEntity momCommentEntity:list){
			remindVo rv=new remindVo();
			type=momCommentEntity.getType();
			rv.setAddTime(TimeUtil.getTimeFormatText(Utils.convertTimestampToString(momCommentEntity.getAddTime())));
			rv.setHeadAddress(momCommentEntity.getHeadAdress()==null?"":momCommentEntity.getHeadAdress());
			rv.setType(type);
			rv.setUserId(momCommentEntity.getUserId());
			rv.setUserName(momCommentEntity.getUserName()==null?"":momCommentEntity.getUserName());
			rv.setDetail(momCommentEntity.getDetail()==null?"":momCommentEntity.getDetail());
			rv.setMonId(String.valueOf(momCommentEntity.getMonId()));
			reList.add(rv);
			//更新成已读状态
			if("2".equals(type)){
				momlikeDao.updateRemindLike(momCommentEntity.getComId());
			}else if("0".equals(type)||"1".equals(type)){
				momCommentDao.updateRemindComment(momCommentEntity.getComId());
			}
		}
		
		
		return reList;
	}

	@Override
	public Map<String,Object> findMonsByMonId(String monId,String userId) throws ParseException {
		Map<String,Object> mapq=new HashMap<String,Object>();
		mapq.put("userId", Long.parseLong(userId));
		mapq.put("monId", Long.parseLong(monId));
		Map<String,Object> resultMap=userMonmentsDao.findMonsByMonId(mapq);
//		resultMap.get("monId");
//		resultMap.get("userId");
//		resultMap.get("nickName");
//		resultMap.get("logoAddress");
//		resultMap.get("mediaAddress");
//		resultMap.get("momLikeNum");
//		resultMap.get("momCommentNum");
//		resultMap.get("detail");
//		resultMap.get("islike");
		resultMap.put("monId",resultMap.get("monId")+"");
		resultMap.put("isLike",resultMap.get("isLike")+"");
		resultMap.put("momCommentNum",resultMap.get("momCommentNum")+"");
		resultMap.put("userId",resultMap.get("userId")+"");
		resultMap.put("momLikeNum",resultMap.get("momLikeNum")+"");
		Timestamp addTime=(Timestamp)resultMap.get("addTime");
		resultMap.put("addTime", TimeUtil.getTimeFormatText(Utils.convertTimestampToString(addTime)));
		//第一帧截图
		String imgStr=resultMap.get("imgStr")==null?"":resultMap.get("imgStr")+"";
		if(imgStr.equals("")){
			resultMap.put("imgStr",this.imgStr);
		}else if(imgStr.equals("1")){
			resultMap.put("imgStr","");
		}else{
			resultMap.put("imgStr",imgStr);
		}
		return resultMap;
	}
	
	

}
